const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        ID_Number: [{
          validator: checkIDCard,
          trigger: 'blur'
        }]
      },
      form: {
        Enterprise_Info_SerialNumber: "",
        Enterprise_Name: '',
        Industry_SubCategory_SerialNumber: '',
        Industry_Category_SerialNumber: '',
        Tax_Number: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
        Children: {
          enterprise_legal_person: [{
            Enterprise_Legal_Person_SerialNumber: "",
            Enterprise_Info_SerialNumber: '',
            Legal_Person: '',
            Mobile: '',
            ID_Number: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }],
          enterprise_invoiceinfo: [{
            Enterprise_InvoiceInfo_SerialNumber: "",
            Enterprise_Info_SerialNumber: '',
            Address: '',
            Mobile: '',
            Bank_Name: '',
            Bank_Account: '',
            Row_Status: '',
            Operation_ID: '',
            Operation_Datetime: '',
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    const EnterpriseInfo_SerialNumber = sessionStorage.getItem("EnterpriseInfo_SerialNumber")
    this.getData(EnterpriseInfo_SerialNumber)
  },
  methods: {
    getData(EnterpriseInfo_SerialNumber) {
      const _this = this
      $.ajax({
        //	url: '../getContractContentByContract_SerialNumberAction',
        url: '../getEnterpriseInfoByEnterpriseInfo_SerialNumberAction',
        type: 'POST',
        data: { Data: EnterpriseInfo_SerialNumber },
        success: function (res) {
          console.log('根据企业编号获取结果', res)
          _this.form = res.enterprise_info[0]
          if (!_this.form.Children.enterprise_legal_person || _this.form.Children.enterprise_legal_person.length == 0) {
            _this.form.Children.enterprise_legal_person = [{
              Enterprise_Legal_Person_SerialNumber: "",
              Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
              Legal_Person: '',
              Mobile: '',
              ID_Number: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
          if (!_this.form.Children.enterprise_invoiceinfo || _this.form.Children.enterprise_invoiceinfo.length == 0) {
            _this.form.Children.contract_receiving_info = [{
              Enterprise_InvoiceInfo_SerialNumber: "",
              Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
              Address: '',
              Mobile: '',
              Bank_Name: '',
              Bank_Account: '',
              Row_Status: '',
              Operation_ID: '',
              Operation_Datetime: '',
            }]
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Modify: JSON.stringify({ enterprise_info: [modify] }) }
          } else {
            data = {
              Modify: JSON.stringify({ enterprise_info: [modify] }),
              Add: JSON.stringify({ enterprise_info: [add] })
            }
          }
          _this.loading = true
          $.ajax({
            url: '../modifyGeneralBusinessAction',
            type: 'POST',
            data: data,
            success: function (res) {
              console.log('提交结果', res)
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1)
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const add = deepClone(this.form)
      const modify = deepClone(this.form)
      let add_enterprise_legal_person = [], modify_enterprise_legal_person = []
      let add_enterprise_invoiceinfo = [], modify_enterprise_invoiceinfo = []
      for (const i in modify.Children.enterprise_legal_person) {
        const obj = modify.Children.enterprise_legal_person[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_enterprise_legal_person.push(obj)
        } else {
          modify_enterprise_legal_person.push(obj)
        }
      }
      for (const i in modify.Children.enterprise_invoiceinfo) {
        const obj = modify.Children.enterprise_invoiceinfo[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_enterprise_invoiceinfo.push(obj)
        } else {
          modify_enterprise_invoiceinfo.push(obj)
        }
      }

      add.Children.enterprise_legal_person = add_enterprise_legal_person
      add.Children.enterprise_invoiceinfo = add_enterprise_invoiceinfo

      modify.Children.enterprise_legal_person = modify_enterprise_legal_person
      modify.Children.enterprise_invoiceinfo = modify_enterprise_invoiceinfo

      if (add_enterprise_legal_person.length == 0 && add_enterprise_invoiceinfo.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_enterprise_legal_person() {
      this.form.Children.enterprise_legal_person.push({
        Enterprise_Legal_Person_SerialNumber: "",
        Enterprise_Info_SerialNumber: this.form.Enterprise_Info_SerialNumber,
        Legal_Person: '',
        Mobile: '',
        ID_Number: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_enterprise_legal_person() {
      const length = this.form.Children.enterprise_legal_person.length
      console.log(length)
      if (length > 1) {
        if (this.form.Children.enterprise_legal_person[length - 1].Enterprise_Info_SerialNumber) {
          for (let i = length - 1; i > 0; --i) {
            if (this.form.Children.enterprise_legal_person[i].Row_Status === '0') {
              this.form.Children.enterprise_legal_person[i].Row_Status = '1'
              break;
            }
          }
        } else {
          this.form.Children.enterprise_legal_person.pop()
        }
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
      // if (this.form.Children.enterprise_legal_person.length > 1) {
      //     this.form.Children.enterprise_legal_person.pop()
      // } else {
      //     this.$message({
      //         message: '不能在删除了',
      //         type: 'warning'
      //     })
      // }
    },
    add_enterprise_invoiceinfo() {
      this.form.Children.enterprise_invoiceinfo.push({
        Enterprise_InvoiceInfo_SerialNumber: "",
        Enterprise_Info_SerialNumber: this.form.Enterprise_Info_SerialNumber,
        Address: '',
        Mobile: '',
        Bank_Name: '',
        Bank_Account: '',
        Row_Status: '',
        Operation_ID: '',
        Operation_Datetime: '',
      })
    },
    remove_enterprise_invoiceinfo() {
      const length = this.form.Children.enterprise_invoiceinfo.length
      console.log(length)
      if (length > 1) {
        if (this.form.Children.enterprise_invoiceinfo[length - 1].Enterprise_Info_SerialNumber) {
          for (let i = length - 1; i > 0; --i) {
            if (this.form.Children.enterprise_invoiceinfo[i].Row_Status === '0') {
              this.form.Children.enterprise_invoiceinfo[i].Row_Status = '1'
              break;
            }
          }
        } else {
          this.form.Children.enterprise_invoiceinfo.pop()
        }
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
      // if (this.form.Children.enterprise_invoiceinfo.length > 1) {
      //     this.form.Children.enterprise_invoiceinfo.pop()
      // } else {
      //     this.$message({
      //         message: '不能在删除了',
      //         type: 'warning'
      //     })
      // }
    },
  }
})